package com.ssm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.entity.log.SystemLog;
import com.ssm.entity.system.Operator;
import com.ssm.entity.system.Privilege;
import com.ssm.service.log.SystemLogService;
import com.ssm.service.system.OperatorService;
import com.ssm.service.system.PrivilegeService;
import com.ssm.util.IpUtil;
import com.ssm.util.MD5;

@Controller
public class LoginController {
	
	@Autowired
	private OperatorService operatorService;
	
	@Autowired
	private PrivilegeService privilegeService;
	
	@Autowired
	private SystemLogService systemLogService;
	
	@RequestMapping("login.html")
	public ModelAndView login(String id, String pwd, String code, HttpServletRequest request) {
		Operator operator = operatorService.getDetail(id);
		ModelAndView mav = new ModelAndView();
		if (operator == null) {
			mav.addObject("errMsg", "此用户名不存在");
			mav.addObject("id", id);
			mav.setViewName("login");
			return mav;
		}
		if (!new MD5().encrypt(pwd).equals(operator.getPwd())) {
			mav.addObject("errMsg", "密码错误");
			mav.addObject("id", id);
			mav.setViewName("login");
			return mav;
		}
		if (operator.getState() != 1) {
			mav.addObject("errMsg", "此用户名异常");
			mav.addObject("id", id);
			mav.setViewName("login");
			return mav;
		}
		
		operator.setLoginTime(new Date());
		operator.setLoginIp(IpUtil.getIpAddress(request));
		operator.setLoginCount(operator.getLoginCount()+1);
		operatorService.update(operator);
		
		request.getSession().setAttribute("user", operator);
		
		SystemLog systemLog = new SystemLog();
		systemLog.setType(1);
		systemLogService.save(systemLog);
		
//		if (operator.getLoginCount() == 1) {
//			mav.setViewName("redirect:updatePwd.html");
//			return mav;
//		}
		
		mav.setViewName("redirect:index.html");
		return mav;
	}
	
	@RequestMapping("index.html")
	public ModelAndView index(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Operator operator = (Operator) session.getAttribute("user");
		List<Privilege> list = privilegeService.initMenu(operator.getRoleId());
		
		Privilege menu = new Privilege();
		menu.setId("P0000000");
		getChildMenu(list, menu);
		
		for (Privilege pri : list) {
			session.setAttribute(pri.getId(), pri.getId());
		}
		ModelAndView mav = new ModelAndView("index");
		mav.addObject(menu);
		return mav;
	}
	
	private void getChildMenu(List<Privilege> list, Privilege menu) {
		if (list == null || menu == null) {
            return;
        }

        String rootMenu = menu.getId();

        // 得到相关的子菜单 获得parentMenu下的子节点
        List<Privilege> childList = new ArrayList<Privilege>();
        for (int i = 0; i < list.size(); i++) {
        	Privilege pri = list.get(i);
            String menuId = pri.getParentId();
            if (menuId.equals(rootMenu)) {
                childList.add(pri);
            }
        }
        menu.setChild(childList);

        // 递归得到所有子节点的子节点
        for (Iterator<Privilege> it = childList.iterator(); it.hasNext();) {
        	Privilege menuitem = it.next();
            getChildMenu(list, menuitem);
        }
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "redirect:";
	}
}
